package com.cqc.zhbjcqc.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cqc.zhbjcqc.R;
import com.cqc.zhbjcqc.activity.MainActivity;
import com.cqc.zhbjcqc.adapter.ListViewBaseAdapter;
import com.cqc.zhbjcqc.bean.Frag2Bean;
import com.cqc.zhbjcqc.fragment.frag2.FragInteract;
import com.cqc.zhbjcqc.fragment.frag2.FragNews;
import com.cqc.zhbjcqc.fragment.frag2.FragPics;
import com.cqc.zhbjcqc.fragment.frag2.FragSubjects;
import com.cqc.zhbjcqc.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2016/9/27.
 */

public class FragMenuLeft extends BaseFragment {
    private static final String TAG = "FragMenuLeft";
    private View rootView;
    private ListView listView;
    private List<String> titleList = new ArrayList<>();
    private LeftMenuAdapter2 adapter;
    //    private List<BaseFragment> fragsList = new ArrayList<>();
    private FragNews fragNews;
    private FragPics fragPics;
    private FragSubjects fragSubjects;
    private FragInteract fragInteract;
    private FragmentTransaction transaction;
    private FragmentManager fm;
    private int currentItemPosition;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag_menu_left, container, false);
        findViews();
        return rootView;
    }

    private void findViews() {
        listView = (ListView) rootView.findViewById(R.id.listView_menu_left);
    }


    public void setData(final List<Frag2Bean.DataBean> dataBeanList) {
        titleList.clear();
        for (int i = 0; i < dataBeanList.size(); i++) {
            String title = dataBeanList.get(i).getTitle();
            titleList.add(title);
        }
        adapter = new LeftMenuAdapter2();
        listView.setAdapter(adapter);

        fm = ((MainActivity) context).getSupportFragmentManager();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //1 改变选中时+选中后item的颜色，
                currentItemPosition = i;
//                adapter.notifyDataSetInvalidated();//都可以
                adapter.notifyDataSetChanged();

                //2 改变frag2中的top标题
                Frag2 frag2 = (Frag2) ((MainActivity) context).getFragHome().getFrag(1);
                frag2.setTitle(i);
                transaction = fm.beginTransaction();
                hideAllFragments();
                switch (i) {
                    case 0:
                        if (fragNews == null) {
                            fragNews = new FragNews(dataBeanList.get(0));
//                            fragsList.add(fragNews);
                            transaction.add(R.id.frame_frag2, fragNews, "FragNews");
                        }
                        transaction.show(fragNews);
                        break;
                    case 1:
                        if (fragSubjects == null) {
                            fragSubjects = new FragSubjects(dataBeanList.get(1));
//                            fragsList.add(fragSubjects);
                            transaction.add(R.id.frame_frag2, fragSubjects, "FragSubjects");
                        }
                        transaction.show(fragSubjects);
                        break;
                    case 2:
                        if (fragPics == null) {
                            fragPics = new FragPics(dataBeanList.get(2));
//                            fragsList.add(fragPics);
                            transaction.add(R.id.frame_frag2, fragPics, "FragPics");
                        }
                        transaction.show(fragPics);
                        break;
                    case 3:
                        if (fragInteract == null) {
                            fragInteract = new FragInteract(dataBeanList.get(3));
//                            fragsList.add(fragInteract);
                            transaction.add(R.id.frame_frag2, fragInteract, "FragInteract");
                        }
                        transaction.show(fragInteract);
                        break;
                }
                menu.toggle();
                transaction.commit();
            }
        });

        /**
         * 默认选中第一个“新闻”，由于下面的几种方法无效，所以我就把listView.setOnItemClickListener（..）
         * position = 0时候的代码取出来，放在了外面。
         */
//        fragNews = new FragNews(dataBeanList.get(0));
//        fragsList.add(fragNews);
//        FragmentTransaction ft = fm.beginTransaction();
//        ft.add(R.id.frame_frag2, fragNews, "FragNews").show(fragNews).commit();

        //默认选中第一个
        listView.setSelection(0);
        listView.post(new Runnable() {
            @Override
            public void run() {
                listView.requestFocusFromTouch();
                listView.setSelection(0);
                adapter.notifyDataSetInvalidated();
            }
        });

//        listView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                listView.requestFocusFromTouch();
//                listView.setSelection(0);
//            }
//        },1000);

//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                listView.requestFocus();
////                listView.requestFocusFromTouch();
//                listView.setItemChecked(1,true);
//                listView.setSelection(1);
//                adapter.notifyDataSetInvalidated();
//            }
//        },1000);
    }


    private void hideAllFragments() {
        if (fragNews != null) {
            transaction.hide(fragNews);
        }
        if (fragPics != null) {
            transaction.hide(fragPics);
        }
        if (fragSubjects != null) {
            transaction.hide(fragSubjects);
        }
        if (fragInteract != null) {
            transaction.hide(fragInteract);
        }
    }


    public class LeftMenuAdapter2 extends BaseAdapter {

        @Override
        public int getCount() {
            return titleList.size();
        }

        @Override
        public Object getItem(int position) {
            return titleList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_menu, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv_menu.setText(titleList.get(i));
            Log.d(TAG, "getView--currentItemPosition=" + currentItemPosition);
            if (i == currentItemPosition) {
                holder.iv_menu.setBackgroundResource(R.mipmap.menu_arr_select);
                holder.tv_menu.setTextColor(Color.RED);
                convertView.setBackgroundResource(R.mipmap.menu_item_bg_select);
            } else {
                holder.iv_menu.setBackgroundResource(R.mipmap.menu_arr_normal);
                holder.tv_menu.setTextColor(Color.WHITE);
                convertView.setBackgroundResource(android.R.color.transparent);
            }

            return convertView;
        }
    }

    public class ViewHolder {

        public ImageView iv_menu;
        public TextView tv_menu;

        public ViewHolder(View convertView) {
            super();
            iv_menu = (ImageView) convertView.findViewById(R.id.iv_menu);
            tv_menu = (TextView) convertView.findViewById(R.id.tv_menu);
        }
    }
}
